package com.maleant.mvvm.basemvvm.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModelProvider;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;

import com.maleant.mvvm.basemvvm.BasicApp;
import com.maleant.mvvm.basemvvm.DataRepository;
import com.maleant.mvvm.basemvvm.db.entity.CommentEntity;
import com.maleant.mvvm.basemvvm.db.entity.ProductEntity;

import java.util.List;

/**
 * Created by ant on 17-12-19.
 */

public class ProductViewModel extends AndroidViewModel {

    private final LiveData<ProductEntity> mObservableProduct;
    public ObservableField<ProductEntity> product = new ObservableField<>();

    private final int mProductId;

    private final LiveData<List<CommentEntity>> mObservableComments;

    public ProductViewModel(@NonNull Application application, DataRepository repository, int mProductId) {
        super(application);
        this.mObservableProduct = repository.loadProduct(mProductId);
        this.mProductId = mProductId;
        this.mObservableComments = repository.loadComments(mProductId);
    }

    public LiveData<ProductEntity> getmObservableProduct() {
        return mObservableProduct;
    }

    public LiveData<List<CommentEntity>> getmObservableComments() {
        return mObservableComments;
    }

    public void setProduct(ObservableField<ProductEntity> product) {
        this.product = product;
    }

    public static class Factory extends ViewModelProvider.NewInstanceFactory {
        private final Application mApplicatoin;
        private final int mProductId;
        private final DataRepository mRepository;

        public Factory(Application application, int productId) {
            mApplicatoin = application;
            mProductId = productId;
            mRepository = ((BasicApp) application).getRepository();
        }
    }
}
